package com.bringway.p2p.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import net.sf.json.JSONObject;

import com.bringway.framework.excel.ExcelSheet;
import com.bringway.global.dao.SysAttechmentDao;
import com.bringway.p2p.constant.P2pConstant;
import com.bringway.p2p.dao.TxDao;
import com.bringway.p2p.service.TxService;
import com.bringway.p2p.util.MsgUtils;
import com.bringway.p2p.util.RsUtils;

import common.Logger;

/**
 * 
 * @author zhai
 * @version 创建时间：2015年12月16日 上午9:34:42
 * @功能：提取现金界面
 */


@Service
@Transactional
public class TxServiceImpl implements TxService{
	
	Logger logger = Logger.getLogger(TxServiceImpl.class);

	@Autowired
	private TxDao txDao;
	
	@Override
	public List queryTxList(JSONObject params) {
		return txDao.queryTxList(params);
	}

	@Override
	public List<ExcelSheet> exportTxData(List<Map<String, Object>> czList) {
		 // 生成导入到EXECL的LIST
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		int headerCount = 1;
		// 点击汇总导出
		List<String[]> bodyList = new ArrayList<String[]>();
		for (int i = 0; i < czList.size(); i++) {
			String[] contents = new String[22];
			JSONObject obj = new JSONObject();
			obj = JSONObject.fromObject(czList.get(i));
			contents[0] = (i + 1)+"";//序号
			contents[1] = obj.optString("GYH");
			contents[2] = obj.optString("TXJE");
			contents[3] = obj.optString("SXF");
			contents[4] = obj.optString("TXSJ");
			contents[5] = obj.optString("DJJE");//COMMENT
			contents[6] = obj.optString("KYJE");
			contents[7] = obj.optString("ZHYE");
			contents[8] = obj.optString("SJHM");
			contents[9] = obj.optString("CardName");
			contents[10] = obj.optString("YHKHM");
			contents[11] = obj.optString("KHH");
			contents[12] = obj.optString("KHHSZD");
			contents[13] = obj.optString("SFZHM");
			contents[14] = obj.optString("YHMC");
			String txState = obj.optString("TX_STATE");
			if("00".equals(txState)){
				contents[15] = "待处理";
			}else if("01".equals(txState)){
				contents[15] = "提现成功";
			}else if("02".equals(txState)){
				contents[15] = "提现失败";
			}
			contents[16] = obj.optString("OPER_DATE");
			bodyList.add(contents);
		}
		ExcelSheet excelSheet = new ExcelSheet();
		excelSheet.setBodyList(bodyList);
		excelSheet.setSheetName("service层实现导出Excel");
		excelSheet.setFromRow(headerCount);
		sheetList.add(excelSheet);
		return sheetList;
	}

	@Override
	public int getTxCount(JSONObject params) {
		return txDao.getTxCount(params);
	}

	@Override
	public String getTxSum(JSONObject params) {
		String sum = txDao.getTxSum(params);
		if(StringUtils.isEmpty(sum)){
			return "0.00";
		}else{
			return sum;
		}
	}

	@Override
	public String updateTxState(JSONObject params) {
		String res = "res_01";
		String ids = params.optString("ids");
		String operState = params.optString("operState");
		logger.info("updateTxState ids is : " + ids);
		logger.info("updateTxState operState is : " + operState);
		String txState = "";
		if("txSuc".equals(operState)){
			txState = "01";
		}else if("txFail".equals(operState)){
			txState = "02";
		}
		
		if(StringUtils.isEmpty(ids)){
			res = "res_01";//数据空
		}else if(StringUtils.isEmpty(txState)){
			res = "res_02";//提现处理类型未知
		}else{
			String[] idArr = ids.split(",");
			for(int i = 0; i< idArr.length; i++){
				txDao.updateTxState(idArr[i], txState);
				Map<String,Object> txInfo = txDao.getTxInfo(idArr[i]);
				if(txInfo.containsKey("TXJE") && txInfo.containsKey("TX_STATE") && txInfo.containsKey("SJHM")){
					String rec_txJe = RsUtils.getStringValueNotNull(txInfo.get("TXJE"),"");
					String rec_txState = RsUtils.getStringValueNotNull(txInfo.get("TX_STATE"),"");
					String rec_sjhm = RsUtils.getStringValueNotNull(txInfo.get("SJHM"),"");
					String inputline = "";//短信发送返回结果
					if(StringUtils.isNotEmpty(rec_sjhm) && "01".equals(rec_txState)){//提现成功
						String content = "尊敬的客户，您好，您的提现"+rec_txJe+"元已汇入您的指定账户，请您查收。如有疑问请致电"+P2pConstant.CompanyPhone+"。";
						String sign="红点e贷";
						logger.info("content : " + content);
						logger.info("sign : " + sign);
						inputline = MsgUtils.fsdx(content, sign, rec_sjhm);
					}else if(StringUtils.isNotEmpty(rec_sjhm) && "02".equals(rec_txState)){//提现失败
						String content = "尊敬的客户，您好，您的提现"+rec_txJe+"元，由于信息不匹配已退回至账户，请您核对信息后重新操作。如有疑问请致电"+P2pConstant.CompanyPhone+"。";
						String sign="红点e贷";
						logger.info("content : " + content);
						logger.info("sign : " + sign);
						inputline = MsgUtils.fsdx(content, sign, rec_sjhm);
					}
					if(inputline.split(",")[0].equals("0") && (inputline.contains("提交成功") || inputline.contains("鎻愪氦鎴愬姛"))){
						logger.info("客户手机号码：" + rec_sjhm + "，短信发送成功!");
    				}else{
    					logger.info("客户手机号码：" + rec_sjhm + "，短信发送失败!");
    				}
				}
			}
			res = "success";
		}
		return res;
	}
	
	
	
}
